/**
 * getStudentList   获取学生列表
 * queryStudentList 查询学生
 * addStudent       添加学生
 * getInfoStudent   获取学生信息
 * updateStudent    修改学生信息
 * deleteStudent    删除学生
 */
// 导入密码加密
const bcrypt = require("bcryptjs")

const classStatement = require("../../common/sql_sentences/class")
const facultyStatement = require("../../common/sql_sentences/faculty")
const gradeStatement = require("../../common/sql_sentences/grade")
const studentStatement = require("../../common/sql_sentences/student")
const repairStatement = require("../../common/sql_sentences/repair")

exports.getStudentList = async (req, res) => {
  try {
    const { grade, s_class, faculty, page, pagesize } = req.query
    // 获取学生总数
    let total,
      sqlStr = studentStatement.selectCount({ grade, s_class, faculty })
    let results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("没有找到学生！", 10005)
    total = results[0].total

    // 获取学生列表
    sqlStr = studentStatement.selectLimit(
      { grade, s_class, faculty },
      page,
      pagesize
    )
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.queryStudentList = async (req, res) => {
  try {
    // 查找学生
    const sqlStr = studentStatement.select(req.query)
    let results = await RunSQL(sqlStr)
    if (!results.length) return res.successSend("没有找到学生！", 10005)
    return res.successSend("查找成功！", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.addStudent = async (req, res) => {
  try {
    const data = req.body

    // 判断学号是否存在
    let sqlStr = studentStatement.selectCount(({ s_number: data.s_number }))
    let results = await RunSQL(sqlStr)

    if (results[0].total) return res.errSend("学号已存在！", 10005)

    // 判断手机号是否存在
    if (data.phone) {
      sqlStr = studentStatement.selectPhone(data.phone)
      let results = await RunSQL
      if (results.length) return res.errSend("手机号已存在！", 10005)
    }

    // 查询学部是否存在
    sqlStr = facultyStatement.selectCount({ name: data.faculty })
    results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("学部不存在！", 10005)

    // 查询年级是否存在
    sqlStr = gradeStatement.selectCount({ name: data.grade })
    results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("年级不存在！", 10005)

    // 查询班级是否存在
    sqlStr = classStatement.selectCount({ name: data.s_class })
    results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("班级不存在！", 10005)

    // 添加学生
    sqlStr = studentStatement.insert({
      ...data,
      password: bcrypt.hashSync(data.password, 10),
    })

    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("添加学生失败！", 10005)
    return res.successSend("添加学生成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoStudent = async (req, res) => {
  try {
    // 获取学生信息
    const sqlStr = studentStatement.idSelect(req.params.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1) return res.successSend("获取学生信息失败！", 10005)
    return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateStudent = async (req, res) => {
  try {
    const data = req.body
    // 判断学号是否存在
    let sqlStr = studentStatement.select(({ s_number: data.s_number }))
    let results = await RunSQL(sqlStr)
    if (results[0]?.id && results[0]?.id !== data.id)
      return res.errSend("学号已存在！", 10005)

    // 判断手机号是否存在
    if (data.phone) {
      sqlStr = studentStatement.selectPhone(data.phone)
      results = await RunSQL
      if (results[0]?.id && results[0]?.id !== data.id)
        return res.errSend("手机号已存在！", 10005)
    }

    // 修改学生信息
    sqlStr = studentStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("修改学生信息失败！", 10005)
    return res.successSend("修改学生信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteStudent = async (req, res) => {
  try {
    // 获取学生信息
    let sqlStr = studentStatement.idSelect(id)
    let results = await RunSQL(sqlStr)
    if (!results.length) return res.successSend("获取学生信息失败！", 10005)

    // 获取学生总数
    sqlStr = repairStatement.selectCount(query)
    results = await RunSQL(sqlStr)
    if (results[0].total) return res.successSend("删除学生失败！有报修单属于该学生！", 10005)

    // 删除学生
    sqlStr = studentStatement.delete(req.params.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("删除学生失败！", 10005)
    return res.successSend("删除学生成功！")
  } catch (error) {
    res.errSend(error)
  }
}
